package com.meiyu.live.tv.audience;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.eventbus.FocusEvent;
import com.meiyu.live.eventbus.NowLiveUpdateEvent;
import com.meiyu.live.net.param.IsfocusParams;
import com.meiyu.live.net.response.BaseResponse;
import com.meiyu.live.util.ImageUtil;
import com.meiyu.live.util.ToastUtil;

import de.greenrobot.event.EventBus;

public class AudienceFinishActivity extends BaseActivity implements View.OnClickListener {

    public final static String ANCHOR_ID = "anchor_id";
    public final static String ANCHOR_NAME = "anchor_name";
    public final static String ANCHOR_AVATAR = "anchor_avatar";
    public final static String AUDIENCE_COUNT = "audience_count";
    public final static String AUDIENCE_DURATION = "audience_duration";
    public final static String AUDIENCE_IS_FOLLOW = "audience_is_follow";

    private ImageLoader imageLoader;
    private DisplayImageOptions options_avatar, options_live;

    private ImageView iv_bg, iv_avatar;
    private TextView tv_name, tv_audience_count;
    private TextView btn_follow, btn_back;
    private ImageView[] iv_hotlive;

    private long anchorId;
    private String anchor_name; //主播名字
    private String anchor_avatar; //主播头像
    private String audience_count;//观看人数
    private long audience_duration;//观看持续时间
    private boolean isFollow;

    private Bitmap bitmapAvatarBlur;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audiencefinish);
        anchorId = getIntent().getLongExtra(ANCHOR_ID, 0);
        anchor_name = getIntent().getStringExtra(ANCHOR_NAME);
        anchor_avatar = getIntent().getStringExtra(ANCHOR_AVATAR);
        audience_count = getIntent().getStringExtra(AUDIENCE_COUNT);
        audience_duration = getIntent().getLongExtra(AUDIENCE_DURATION, 0);
        isFollow = getIntent().getBooleanExtra(AUDIENCE_IS_FOLLOW, false);
        init();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().post(new NowLiveUpdateEvent());
        super.onDestroy();
    }

    private void init() {
        imageLoader = ImageLoader.getInstance();
        options_avatar = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_def_header)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.icon_def_header)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.icon_def_header)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .build();
        options_live = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.image_loading)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.image_loading)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.image_loading)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .build();

        iv_bg = (ImageView) findViewById(R.id.iv_bg);
        iv_avatar = (ImageView) findViewById(R.id.iv_avatar);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_audience_count = (TextView) findViewById(R.id.tv_audience_count);
        iv_hotlive = new ImageView[]{
                (ImageView) findViewById(R.id.iv_hotlive0),
                (ImageView) findViewById(R.id.iv_hotlive1),
                (ImageView) findViewById(R.id.iv_hotlive2),
                (ImageView) findViewById(R.id.iv_hotlive3)
        };

        btn_follow = (TextView) findViewById(R.id.btn_follow);
        btn_follow.setOnClickListener(this);
        btn_follow.setVisibility(isFollow ? View.INVISIBLE : View.VISIBLE);
        btn_back = (TextView) findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);

        update();
        getData();
    }

    private void update() {
        imageLoader.displayImage(anchor_avatar, iv_avatar, options_avatar, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, final Bitmap bitmap) {
                if (bitmap != null) {
                    if (Build.VERSION.SDK_INT >= 17) {
                        if (bitmapAvatarBlur != null && !bitmapAvatarBlur.isRecycled()) {
                            bitmapAvatarBlur.recycle();
                            iv_bg.setImageResource(R.color.black);
                        }
                        try {
                            bitmapAvatarBlur = ImageUtil.blurBitmap(AudienceFinishActivity.this, bitmap);
                            iv_bg.setImageBitmap(bitmapAvatarBlur);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        if (bitmapAvatarBlur != null && !bitmapAvatarBlur.isRecycled()) {
                            bitmapAvatarBlur.recycle();
                            iv_bg.setImageResource(R.color.black);
                        }
                        new Thread() {
                            @Override
                            public void run() {
                                bitmapAvatarBlur = ImageUtil.blurBitmapOld(AudienceFinishActivity.this, bitmap);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        iv_bg.setImageBitmap(bitmapAvatarBlur);
                                    }
                                });
                            }
                        }.start();
                    }
                }
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });
        tv_name.setText(anchor_name);
        tv_audience_count.setText(audience_count);
    }

    private void getData() {
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.UrlParams.CLOSEFAN);
//        request(b, HotLiveResponse.class, new Listener<HotLiveResponse>() {
//            @Override
//            public void onResponse(HotLiveResponse hotLiveResponse) {
//                if (hotLiveResponse != null && hotLiveResponse.getHash() != null) {
//                    for (int i = 0; i < hotLiveResponse.getHash().size() && i < iv_hotlive.length; i++) {
//                        imageLoader.displayImage(hotLiveResponse.getHash().get(i).getImage_url(), iv_hotlive[i], options_live);
//                    }
//                }
//            }
//        });
    }

    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            finish();
        } else if (view == btn_follow) {
            int a[] = {1, 1};
            request(new IsfocusParams(anchorId, a), BaseResponse.class, new Response.Listener<BaseResponse>() {
                @Override
                public void onResponse(BaseResponse onlyStatusResponse) {
                    btn_follow.setEnabled(false);
                    btn_follow.setText("已关注");
                    ToastUtil.show("关注成功");
                    EventBus.getDefault().post(new FocusEvent(true, anchorId));
                }
            });
        }
    }
}
